package com.eystar.pdc.controller;

import java.util.List;

import com.eystar.ext.route.ControllerBind;
import com.eystar.pdc.aop.auth.AuthAnno;
import com.eystar.pdc.aop.auth.AuthAnno.AuthType;
import com.eystar.pdc.model.Fav;

/**
 * 收藏管理
 * @author Administrator
 *
 */
@ControllerBind(controllerKey="/fav", viewPath="/pdc")
@AuthAnno(type = AuthType.Auth, res = "fav")
public class FavController extends BaseController {

	/**
	 * 返回已经收藏的菜单列表
	 * @throws Exception
	 */
	public void index() throws Exception{
		Fav fav = getModel(Fav.class);
		fav.set("create_user", getCurrentUserName());
		List<Fav> favs = Fav.dao.findFavs(fav);
		renderJson(favs);
	}
	
	/**
	 * 收藏获取取消收藏
	 * @throws Exception
	 */
	public void modify() throws Exception{
		boolean fav = getParaToBoolean("fav"); // true=当前是收藏，点击后应该是取消收藏，就是执行删除。false表示执行添加
		Fav fav2 = getSaveModel(Fav.class);
		fav2.set("res_id", get("resId"));
		fav2.set("name", get("name"));
		fav2.set("url", get("dataId"));
		Fav.dao.deleteFav(fav2); // 不管是收藏或者取消收藏，都先给删除了
		if(!fav) { 
			fav2.save();
		}
		renderJson(ok());
	}
}